package com.romadan.gui.controller.impl;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Toggle;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;

import com.romadan.core.linearanalys.logic.LParameters;
import com.romadan.general.settings.ElementUnits;
import com.romadan.gui.controller.AbstractController;

public class DiagramWindow extends AbstractController implements Initializable {
	private static final double VBOX_PADDING = 10;
	private static final double BUTTON_WIDTH = 120;
	private static final double BUTTON_HEIGHT = 30;
	private static final double CHART_PREF_HEIGHT = 414;
	private static final double CHART_PREF_WIDTH = 518;
	private final ToggleGroup groupButtons = new ToggleGroup();
	private String voltage = "vGs";

	public void initialize(URL arg0, ResourceBundle arg1) {
		diagramWindow = this;
		vBox.setPadding(new Insets(VBOX_PADDING));
		vBox.setSpacing(10);

		LParameters l = lParameters.get(0);
		for (String elName : l.getIntElements().getAllElements().keySet()) {
			ToggleButton button = new ToggleButton();
			button.setToggleGroup(groupButtons);
			button.setText(elName);
			button.setId(elName);
			button.setPrefWidth(BUTTON_WIDTH);
			button.setPrefHeight(BUTTON_HEIGHT);

			groupButtons.selectedToggleProperty().addListener(
					new ChangeListener<Toggle>() {
						public void changed(
								ObservableValue<? extends Toggle> ov,
								Toggle toggle, Toggle new_toggle) {
							refreshChart();
						}
					});

			vBox.getChildren().add(button);
		}

		groupButtons.selectToggle(groupButtons.getToggles().get(0));
	}

	public void setVisibleForRB(boolean state) {
		vGs.visibleProperty().set(state);
		vDs.visibleProperty().set(state);
	}

	@FXML
	private VBox vBox;
	@FXML
	private RadioButton vGs;
	@FXML
	private RadioButton vDs;
	@FXML
	private AnchorPane paneForChart;

	@FXML
	private void changeVoltage(ActionEvent event) {
		if (event.getSource().equals(vGs)) {
			vDs.setSelected(false);
			vGs.setSelected(true);
			voltage = vGs.getId();
		} else {
			vGs.setSelected(false);
			vDs.setSelected(true);
			voltage = vDs.getId();
		}
		refreshChart();
	}

	private void refreshChart() {
		ToggleButton button = (ToggleButton) groupButtons.getSelectedToggle();
		if (button != null) {
			String elName = button.getId();
			String title = "Values of parameter \"" + elName + "\"";
			String xLabel = (voltage.equals("vGs")) ? "Vgs" : "Vds";
			String yLabel = elName + ", " + ElementUnits.getUnit(elName);

			List<Double> yValues = new ArrayList<Double>();
			List<Double> xValues = new ArrayList<Double>();

			for (LParameters l : lParameters) {
				double yValue = l.getIntElements().getAllElements().get(elName);
				yValues.add(yValue);
				double xValue = (voltage.equals("vGs")) ? l
						.getVoltageGS() : l.getVoltageDS();
				xValues.add(xValue);
			}

			LineChart chart = getLineChart(title, xLabel, yLabel, xValues,
					yValues);
			chart.setPrefHeight(CHART_PREF_HEIGHT);
			chart.setPrefWidth(CHART_PREF_WIDTH);
			chart.setLayoutX(0);
			chart.setLayoutY(-2);
			if (paneForChart.getChildren().size() > 2)
				paneForChart.getChildren().remove(2);
			paneForChart.getChildren().add(2, chart);
		}
	}
}
